import {request} from '../../request/index.js'

Page({
    data:{
        leftMenuList:[],
        rightMenuList:[],
        currentIndex:0,
        scrollTop:0
    },
    cates:[],
    onLoad:function(options){

        const cates = wx.getStorageSync('cates');
        if(!cates){
            this.getCates();
        }else{
            if(Date.now() - cates.time > 1000 * 10){
                this.getCates();
            }else{
                this.cates = cates.data;
                let leftMenuList = this.cates.map((v)=>{return v.cat_name})
                let rightMenuList = this.cates[0].children;
                this.setData({
                    leftMenuList,
                    rightMenuList
                });
            }
        }
        
    },
    getCates() {
        request({url:"https://api-hmugo-web.itheima.net/api/public/v1/categories"}).then((res)=>{
            this.cates = res.data.message;

            wx.setStorageSync('cates',{time: Date.now(),data:this.cates})

            let leftMenuList = this.cates.map((v)=>{return v.cat_name})
            let rightMenuList = this.cates[0].children;
            this.setData({
                leftMenuList,
                rightMenuList
            });
        })
    },
    handleItemTap(e){
        const {index} = e.currentTarget.dataset;
        let rightMenuList = this.cates[index].children;
        this.setData({
            currentIndex:index,
            rightMenuList,
            scrollTop:0
        });
    }
    

})